package com.eeepay.eeepay_v2.api;

import android.support.annotation.NonNull;

import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;
import com.eeepay.eeepay_v2.util.MathUtils;
import com.eeepay.eeepay_v2.util.TimeUtils;
import com.eeepay.v2_library.log.LogUtils;

import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.OutAccountApiProto;
import cn.eeepay.api.grpc.nano.OutAccountApiServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * 描述：根据不同的科目获取不同账户余额
 * 1 账户余额
 * 2 活动补贴账号余额
 * 作者：zhuangzeqin
 * 时间: 2017/6/23-17:00
 * 邮箱：zzq@eeepay.cn
 */
public class AgentAccountBalanceRequestAPI {
    private static final int GET_OUT_ACCOUNT_BALANCE = 1;//获得账户信息请求任务id
    private ResultCallBack resultCallBack;//设置监听接口

    public AgentAccountBalanceRequestAPI() {
    }

    /**
     * 设置监听回调
     *
     * @param resultCallBack
     */
    public void setResultCallBack(@NonNull ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    /**
     * 开始获取账号余额信息
     *
     * @param subjectNoType 科目编号
     */
    public void getAgentAccountBalanceInfo(@SubjectNoTypeDef.SubjectNoType final String subjectNoType) {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }
        resultCallBack.oninit();//请求前做一些初始化操作
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, GET_OUT_ACCOUNT_BALANCE, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                OutAccountApiServiceGrpc.OutAccountApiServiceBlockingStub stub = OutAccountApiServiceGrpc.newBlockingStub(channel)
                        .withDeadlineAfter(Constant.grpc_time_out, TimeUnit.SECONDS);
                OutAccountApiProto.OutAccountReq request = new OutAccountApiProto.OutAccountReq();
                request.accountType = "A";//A代表代理商  M代表商户
                request.userId = UserInfo.getUserInfo2SP().getAgentNo();
                request.subjectNo = subjectNoType;//科目
                request.currencyNo = "1";
                request.recordDate1 = TimeUtils.getNowDate();
                request.recordDate2 = TimeUtils.getNowDate();
                request.accountOwner = "000001";
                request.selectType = "2";
                OutAccountApiProto.OutAccountListRes response = stub.getOutAccountBalance(request);
                return response;
            }

            @Override
            public void updateUI(Object result, int taskId) {
                resultCallBack.onUnInit();//返初始化操作
                if (result == null) {
                    resultCallBack.onFail("查询账户信息失败");
                    return;
                }
                OutAccountApiProto.OutAccountListRes response = (OutAccountApiProto.OutAccountListRes) result;
                LogUtils.d("result = " + response);
                if ("false".equals(response.rpcStatus)) {
                    resultCallBack.onFail(response.rpcMsg);
                    return;
                }
                OutAccountApiProto.OutAccountRes[] data = response.outAccountRes;
                //成功返回账号余额取值数据将返回给外部
                if (resultCallBack != null) {
                    resultCallBack.onSucess(MathUtils.String2Float(data[0].avaliBalance) + "", subjectNoType);
                }
            }
        });
    }

    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {
        void oninit();//初始化

        void onFail(String msg);//失败返回；提示语

        void onSucess(String avaliBalance, @SubjectNoTypeDef.SubjectNoType final String subjectNoType);//成功返回账号余额取值数据

        void onUnInit();//返初始化操作
    }
}
